﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace CommonUtil
{
    public class ExecCmd
    {
        #region exec dos cmd
        private static string Exec(string command)
        {
            string info = "";
            Process cmd = new Process();
            try
            {
                cmd.StartInfo.FileName = "cmd";
                cmd.StartInfo.RedirectStandardOutput = true;
                cmd.StartInfo.RedirectStandardInput = true;
                cmd.StartInfo.UseShellExecute = false;      //是否指定操作系统外壳进程启动程序
                cmd.StartInfo.CreateNoWindow = true;        //不显示dos命令行窗口

                cmd.Start();
                cmd.StandardInput.WriteLine(command);       //run real cmd
                cmd.StandardInput.WriteLine("exit");
                info = cmd.StandardOutput.ReadToEnd();
            }
            catch (Exception ex)
            {
                info = "";
                Trace.WriteLine(ex.Message);
            }
            finally
            {
                if (cmd != null) cmd.Close();
            }
            return info;
        }
        #endregion

        #region SQL用BCP导出到XML
        /// <summary>
        /// SQL用BCP导出到XML
        /// </summary>
        /// <param name="BcpSql">T-SQL语句</param>
        /// <param name="SavePath">保存路径，如: d:\aa.xml</param>
        /// <param name="ServerIp">服务器IP</param>
        /// <param name="DbLogin">用户名</param>
        /// <param name="Pwd">密码</param>
        /// <returns>成功返回True</returns>
        public static bool ExecBcp(string BcpSql, string SavePath, string ServerIp, string DbLogin, string Pwd)
        {
            bool result = true;
            string info = "";

            string logPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\synLog\\";
            if (!Directory.Exists(logPath)) Directory.CreateDirectory(logPath);
            string logFile = logPath + Guid.NewGuid().ToString("N").ToUpper() + ".log";

            string command = "bcp \"%BcpSql%\" queryout %SavePath% -c -t -S %ServerIp% -U %DbLogin% -P %Pwd% -b 30000 -a 8192 -e %LogPath%";
            command = command.Replace("%BcpSql%", BcpSql);
            command = command.Replace("%SavePath%", SavePath);
            command = command.Replace("%ServerIp%", ServerIp);
            command = command.Replace("%DbLogin%", DbLogin);
            command = command.Replace("%Pwd%", Pwd);
            command = command.Replace("%LogPath%", logFile);

            Process cmd = new Process();
            try
            {
                cmd.StartInfo.FileName = "cmd";
                cmd.StartInfo.RedirectStandardOutput = true;
                cmd.StartInfo.RedirectStandardInput = true;
                cmd.StartInfo.UseShellExecute = false;      //是否指定操作系统外壳进程启动程序
                cmd.StartInfo.CreateNoWindow = true;        //不显示dos命令行窗口

                cmd.Start();
                cmd.StandardInput.WriteLine(command);
                cmd.StandardInput.WriteLine("exit");
                info = cmd.StandardOutput.ReadToEnd();

                using (StreamReader sr = new StreamReader(logFile, Encoding.Default))
                {
                    if (sr.ReadToEnd().Length > 0) result = false; //error
                }
            }
            catch (Exception ee)
            {
                result = false;
                Logger.Record(String.Format("ExecBcp异常: {0}~{1}~{2}", ee.Message, ee.Source, ee.StackTrace), LogType.Error);
            }
            finally { if (cmd != null) cmd.Close(); }


            try
            {
                if (File.Exists(logFile)) File.Delete(logFile);
            }
            catch { }

            return result;  
        }
        #endregion

    }
}
